package com.cdzy.controller;

import com.cdzy.common.ApiResponse;
import com.cdzy.pojo.User;
import com.cdzy.pojo.UserVo;
import com.cdzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

// @Controller、@Service、@Repository作用是一模一样的

@RestController // 1、加入容器 2、把方法的返回值当成响应
@RequestMapping("/user")
@CrossOrigin    // 解决跨域问题
public class UserController {
    @Autowired  // 依赖注入
    UserService userService;

    // 查询用户列表
    @PostMapping("/list")
    public ApiResponse<?> list(@RequestBody User user) {
        UserVo userVo = userService.getUserList(user);

        return ApiResponse.success(userVo);
    }

    // 查询用户详情
    @GetMapping("/detail/{id}")
    public ApiResponse<?> detail(@PathVariable int id) {
        User userDetail = userService.getUserDetail(id);
        return ApiResponse.success(userDetail);
    }

    // 新增用户
    @PostMapping("/add")
    public ApiResponse<?> add(@RequestBody User user) {
        userService.addUser(user);
        return ApiResponse.success();
    }

    // 删除用户
    @PostMapping("/delete")
    public ApiResponse<?> delete(@RequestBody User user) {
        userService.removeUser(user.getUserId());
        return ApiResponse.success();
    }

    // 更新用户
    @PostMapping("/update")
    public ApiResponse<?> update(@RequestBody User user) {
        userService.modifyUser(user);
        return ApiResponse.success();
    }

    // 批量删除用户
    @PostMapping("/deleteBatch")
    public ApiResponse<?> deleteBatch(@RequestBody User user) {
        userService.removeUserBatch(user.getIdList());
        return ApiResponse.success();
    }

    // 用户登录
    @PostMapping("/login")
    public ApiResponse<?> login(@RequestBody User user) {
        User loginUser = userService.login(user);
        return ApiResponse.success(loginUser);
    }

}
